package de.voss.audiomixer.legacy;

import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiEvent;
import javax.sound.midi.Sequence;
import javax.sound.midi.ShortMessage;
import javax.sound.midi.Track;

public class SequenceGeneration
{

	public static Sequence genSeq() throws InvalidMidiDataException
	{
		// Partitur {{Tonhoehe, DauerInViertelNoten, AnzahlWdh},...}
		final int DATA[][] = { { 0,1,1 }, { 0,1,1 }, { 0,1,1 }, { 0,1,1 }, { 0,1,1 }, { 0,1,1 }, { 0,1,1 }, { 0,1,1 },
				{ 0,1,1 }, { 0,1,1 }, { 0,1,1 }, { 0,1,1 }, { 0,1,1 }, { 0,1,1 }, { 0,1,1 }, { 0,1,1 }, { 0,1,1 },
				{ 0,1,1 }, { 0,1,1 }, { 0,1,1 }, { 0,1,1 }, { 0,1,1 }, { 0,1,1 }, { 0,1,1 }, { 0,1,1 }, { 0,1,1 },
				{ 0,1,1 }, { 0,1,1 }
		/*
		 * {0, 1, 1}, //C {0, 1, 1}, //D {0, 1, 1}, //E {0, 1, 1}, //F {0, 2, 2}, //G,G {0, 1, 4}, //A,A,A,A {0, 4, 1},
		 * //G {0, 1, 4}, //A,A,A,A {0, 4, 1}, //G {0, 1, 4}, //F,F,F,F {0, 2, 2}, //E,E {0, 1, 4}, //D,D,D,D {0, 4, 1}
		 * //C
		 */
		};
		// Sequence bauen
		final int PPQS = 16;
		final int STAKKATO = 4;
		Sequence seq = new Sequence(Sequence.PPQ, PPQS);
		Track track = seq.createTrack();
		long currentTick = 0;
		ShortMessage msg;
		// Kanal 0 auf "EnsembleStrings" umschalten
		msg = new ShortMessage();
		// msg.setMessage(ShortMessage.PROGRAM_CHANGE, 0, 0, 0);
		// track.add(new MidiEvent(msg, currentTick));
		// Partiturdaten hinzufügen BASSDRUM
		for (int i = 0; i < DATA.length; ++i)
		{
			for (int j = 0; j < DATA[i][2]; ++j)
			{ // Anzahl Wdh. je Note
				msg = new ShortMessage();
				msg.setMessage(ShortMessage.NOTE_ON,0,DATA[i][0],64);
				track.add(new MidiEvent(msg, currentTick));
				currentTick += PPQS * DATA[i][1] - STAKKATO;
				msg = new ShortMessage();
				msg.setMessage(ShortMessage.NOTE_OFF,0,DATA[i][0],0);
				track.add(new MidiEvent(msg, currentTick));
				currentTick += STAKKATO;
			}
		}

		// Partiturdaten hinzufügen SNAREDRUM
		final int SNAREOB = 16;
		long currentTickSnare = 0 + SNAREOB;
		for (int i = 0; i < DATA.length / 2; ++i)
		{
			for (int j = 0; j < DATA[i][2]; ++j)
			{ // Anzahl Wdh. je Note
				msg = new ShortMessage();
				msg.setMessage(ShortMessage.NOTE_ON,0,DATA[i][0] + 1,64);
				track.add(new MidiEvent(msg, currentTickSnare));
				currentTickSnare += PPQS * DATA[i][1] * 2 - STAKKATO;
				msg = new ShortMessage();
				msg.setMessage(ShortMessage.NOTE_OFF,0,DATA[i][0] + 1,0);
				track.add(new MidiEvent(msg, currentTickSnare));
				currentTickSnare += STAKKATO;
			}
		}

		// Partiturdaten hinzufügen HIHAT
		final int HIHATOB = 8;
		long currentTickHihat = 0 + HIHATOB;
		for (int i = 0; i < DATA.length; ++i)
		{
			for (int j = 0; j < DATA[i][2]; ++j)
			{ // Anzahl Wdh. je Note
				msg = new ShortMessage();
				msg.setMessage(ShortMessage.NOTE_ON,0,DATA[i][0] + 2,64);
				track.add(new MidiEvent(msg, currentTickHihat));
				currentTickHihat += PPQS * DATA[i][1] - STAKKATO;
				msg = new ShortMessage();
				msg.setMessage(ShortMessage.NOTE_OFF,0,DATA[i][0] + 2,0);
				track.add(new MidiEvent(msg, currentTickHihat));
				currentTickHihat += STAKKATO;
			}
		}

		return seq;
	}
}
